#include <allegro.h>
#include <stdio.h>
#include "stage2.h"
#include <apeg.h>
#define GFX_W 800 
#define GFX_H 600
#define NB_IMAGES 4
#define NB_IMAGES_attack 16

/*
 * module background
 */

BITMAP* load_background_bmp(char chemin_background[])
{
	BITMAP* Background;

	Background=load_bitmap(chemin_background, NULL);
	if(Background == NULL){
		set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
		allegro_message("Unable to load image background!\n%s\n", allegro_error);
		return (NULL);
}
	return (Background);

}
BITMAP* load_background1_bmp(char chemin_background_col[])
{
	BITMAP* BackgroundCol;

	BackgroundCol=load_bitmap(chemin_background_col, NULL);
	if(BackgroundCol == NULL){
		set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
		allegro_message("Unable to load image background col!\n%s\n", allegro_error);
		return (NULL);
	}
	return (BackgroundCol);

}



void draw_background(BITMAP *Background,BITMAP*Buffer,Scroller scrol)
{
	blit(Background, Buffer, 0, 0, scrol.x, scrol.y, Background->w, Background->h);
}

void destroy_background(BITMAP * Background)
{
	destroy_bitmap(Background);
}

/*
 * module mario
 */

void load_sandra(Sandra *sandra)
{
	int i;
	char buf[50];
	
	for (i = 0; i < NB_IMAGES; i++) 
	{
		sprintf(buf, "../images/right%d.bmp", i);
		sandra->img_right[i] = load_bitmap(buf, NULL);
		sprintf(buf, "../images/left%d.bmp", i);
		sandra->img_left[i] = load_bitmap(buf, NULL);
	}
	for (i=0 ;i<NB_IMAGES_attack;i++)
	{	
		sprintf(buf, "../images/Attaque du Sandra/right%d.bmp", i);
		sandra->attack_right[i] = load_bitmap(buf, NULL);//attack epais	
	}
	sandra->jump_right = load_bitmap("../images/jump_right.bmp",NULL);
	sandra->jump_left = load_bitmap("../images/jump_left.bmp",NULL);
	sandra->stand_right = load_bitmap("../images/stand_right.bmp",NULL);
	sandra->stand_left = load_bitmap("../images/stand_left.bmp",NULL); 
	sandra->aiming_right = load_bitmap("../images/aiming_right.bmp",NULL); 
	sandra->aiming_left = load_bitmap("../images/aiming_left.bmp",NULL); 	
	
}

void destroy_sandra(Sandra *sandra)
{
	int i;
	for (i = 0; i < NB_IMAGES; i++) {
		destroy_bitmap(sandra->img_right[i]);
		destroy_bitmap(sandra->img_left[i]);
	}

}
void init_sandra(Sandra *sandra)
{	
	sandra->direction = 0;
	sandra->x = 50;
	sandra->y = 350;
}


//Scroller
void init_scroller(Scroller *scrol)
{
	
scrol->x=0;
scrol->y=0;	
	
	}
void move_sandra_right(Sandra *sandra,int *compteur,BITMAP * Background,int pix1,Arc *arc,Scroller *scrol)
{	
	arc->y=(sandra->y);

	if (key[KEY_RIGHT]  && pix1!=255 && scrol->x> (-Background->w +800)/*pour ne pas dépasser la map*/)
	{
	*compteur+=1;
        scrol->x-=1;
	sandra->direction = 0; //right
	sandra->d=1;
	sandra->cas=0;
				//printf("\n pixel_right:%d",pix1);
	}
	
	
	if (key[KEY_RIGHT]  && pix1!=255 && key[KEY_SPACE] )
	{
	*compteur+=1;
	(arc->x)+=5;//dépalcement de l'épais
	(scrol->x)-=1;//dépalacement camera
	sandra->direction = 0; //right
	sandra->d=1;
	sandra->cas=1;
	}
	}

void move_sandra_left(Sandra *sandra,int *compteur,BITMAP * Background,int pix2,Scroller *scrol)
	{
	if (key[KEY_LEFT] && pix2 != 255 && scrol->x<=0) 
	{
	*compteur+=1;
        scrol->x+=1;
	sandra->direction = 1; //left
	sandra->d=1;
		
	}

}
void jump_sandra(Sandra *sandra,BITMAP * Background,int pix3,Scroller *scrol)
{

	if (key[KEY_UP] && (sandra->y > 0) && pix3!=255) 
	{
	(sandra->y)-=5;
		
	sandra->direction = 2; //jump
	sandra->v=(-16);
		
	if (key[KEY_RIGHT] && key[KEY_UP])
	{
		
	(sandra->y)-=5;
		
	sandra->direction = 2; //jump
	sandra->v=(-11);
	(scrol->x)+=1;		

	}	
	//printf ("up :%d \n",pix3);
	}
}

void stand_sandra(Sandra *sandra,BITMAP * Background, Arc *arc)
{
	if (!key[KEY_RIGHT] && !key[KEY_LEFT] && !((mouse_b & 1) )&& !((mouse_b & 2)))
	{
	sandra->d=0;
	(arc->x)=(sandra->x);
	}
}

void attack_epe(Sandra *sandra,int *compteur,int pix2)
{

	if ((mouse_b & 2)  )
	{	
		
		sandra->y = 380;

		*compteur+=1;
		(sandra->x)+=1;
		sandra->direction=3;// attack
		sandra->d= 1;
	}



}


void attack_arc(Sandra *sandra,Arc *arc)
{
	
	arc->y=(sandra->y);
	if ( mouse_b & 1) 
	{				
	(arc->x)+=4;
			
	sandra->direction=4;// attack
	sandra->d= 1;	
		
	}
	

}
void move_sandra(Sandra *sandra,int *compteur,BITMAP * Background,int pix1,int pix2,int pix3,int pix4,Arc *arc,Scroller *scrol,int *key_down)
{	
	
	if (*key_down==1)
	{
	move_sandra_right(sandra,compteur,Background,pix1,arc,scrol);
  	*key_down=0;
	}
	else if (*key_down==2)
	{
	move_sandra_left(sandra,compteur,Background,pix2,scrol);
	*key_down=0;
	}
	else if (*key_down==3)
	{	
	jump_sandra(sandra,Background,pix3,scrol);
	*key_down=0;
	}


	stand_sandra(sandra,Background ,arc);
	attack_epe(sandra,compteur,pix2);
	attack_arc(sandra,arc);
	//add gravity*/
        printf("\n pixel_down:%d",pix4);
	if (pix4!=255)
 	{
	(sandra->y)+=1;	
	}
	}
void draw_sandra(Sandra sandra,BITMAP *Buffer, int compteur,Arc arc)
{
	static int last_direction;
	 if (sandra.d== 0 && sandra.direction == 0 )
	{
		draw_sprite(Buffer, sandra.stand_right,sandra.x, sandra.y);
		last_direction=0;
	}
	if (sandra.d== 0 && sandra.direction == 1)
	{	
		draw_sprite(Buffer, sandra.stand_left,sandra.x, sandra.y);
		last_direction=0;
	}

	//attack
	if (sandra.d== 1 && sandra.direction == 3)
	{
		draw_sprite(Buffer, sandra.attack_right[compteur % (NB_IMAGES_attack-1) ],sandra.x, sandra.y);
	}
	if (sandra.d== 0 && sandra.direction == 3)
	{
		draw_sprite(Buffer, sandra.stand_right,sandra.x, sandra.y);
	}
	if (sandra.d== 1 && sandra.direction == 4)
	{
		draw_sprite(Buffer, sandra.aiming_right,sandra.x, sandra.y);
		draw_sprite(Buffer, arc.img,arc.x, arc.y);
	}
	if (sandra.d== 0 && sandra.direction == 4)
	{
		draw_sprite(Buffer, sandra.stand_right,sandra.x, sandra.y);
		
	}
	
	if (sandra.d== 1 && sandra.direction == 0)
	{ 
		if (sandra.cas==0)
	{
		draw_sprite(Buffer, sandra.img_right[compteur % (NB_IMAGES-1) ],sandra.x, sandra.y);
		last_direction=0;
	}
		else if (sandra.cas==1)
	{
		draw_sprite(Buffer, sandra.img_right[compteur % (NB_IMAGES-1) ],sandra.x, sandra.y);
		draw_sprite(Buffer, arc.img,arc.x, arc.y);
		last_direction=0;
	}
	}

	if(sandra.d== 1 && sandra.direction == 1)
	{ 
		draw_sprite(Buffer, sandra.img_left[compteur % (NB_IMAGES-1) ],sandra.x, sandra.y);
		last_direction=1;
	}
	if (sandra.direction == 2 && last_direction==0)
	{
		draw_sprite(Buffer, sandra.jump_right,sandra.x, sandra.y);
	}
	if (sandra.direction == 2 && last_direction==1) 
	{
		draw_sprite(Buffer, sandra.jump_left,sandra.x, sandra.y);
	}
}






int get_pixel(BITMAP* Buffer,int x,int y)
{
	int tmp;
	tmp=getpixel(Buffer,x,y);
	return((getr(tmp) + getb(tmp) + getg(tmp)) / 3);
}

//Music


//Pause 
int pause_jeux(BITMAP* BackgroundC,BITMAP* BackgroundS,BITMAP* BackgroundO,BITMAP *BackgroundQ,BITMAP* Buffer,int *b,int *c)
{
if (key[KEY_DOWN]){*c+=1;}
if (key[KEY_SPACE] && (*c % 4 ==1)){*b=0;return *b;}
if  (*c % 4 ==0){draw_sprite(Buffer,BackgroundC,70,70);}
if (*c % 4==1){draw_sprite(Buffer,BackgroundS,70,70);}
if (*c %4 ==2){draw_sprite(Buffer,BackgroundO,70,70);}
if (*c %4 ==3){draw_sprite(Buffer,BackgroundQ,70,70);}
readkey();
return *b;
}

//Music
SAMPLE* load_music(char chemin_music[])
{
	SAMPLE* music;

	music=load_wav(chemin_music);
	if (!music) 
	{
	allegro_message("ERROR LOAD MUSIC: %s", allegro_error);
	readkey();
	return 0;
	}
	return(music);


}
void play_music(SAMPLE *music)
{
	int son_music=0;
	
	voice_set_priority(son_music, 150);
	son_music=play_sample(music,128,128,1000,0) ;
	
	
	
}
void play_musicc(SAMPLE *wolf)
{
	
	int wolff=0;
	
	voice_set_priority(wolff, 255);
	wolff=play_sample(wolf,128,128,1000,0) ;
	
}


void destroy_music(SAMPLE *music)
{
	
	destroy_sample(music);	
}


